package com.kurumi.util;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import com.kurumi.pojo.MedicalHomepage;

public class MedicalHomepageUtil {

	public static MedicalHomepage getMedicalHomepage(String json){
		MedicalHomepage homepage = new MedicalHomepage();
		 if(StringUtils.isBlank(json)) {
			 return homepage;
		 }
		 
		 Map<String,Object> jsonMap =JsonUtil.jsonToPojo(json,Map.class);
		 if(MapUtils.isEmpty(jsonMap)) {
			 return homepage;
		 }
		 
		 Map<String,Object> basicInfoMap = MapUtils.getMap(jsonMap, "basicInfo");
		 homepage.setVisitGuid(MapUtils.getString(basicInfoMap, "visitGuid"));
		 homepage.setMrId(MapUtils.getString(basicInfoMap, "mrId"));
		 homepage.setOnlyId(MapUtils.getString(basicInfoMap, "onlyId"));
		 homepage.setInhosTimes(MapUtils.getInteger(basicInfoMap, "visitNumber"));
		 homepage.setPatientName(MapUtils.getString(basicInfoMap, "patientName"));
		 homepage.setMedicalPaymentCode(MapUtils.getString(basicInfoMap, "medicalPayTypeCode"));
		 homepage.setMedicalPaymentType(MapUtils.getString(basicInfoMap, "medicalPayTypeName"));
		 homepage.setMedicalPaymentType(MapUtils.getString(basicInfoMap, "medicalPayTypeName"));
		 homepage.setSexCode(MapUtils.getString(basicInfoMap, "sexCode"));
		 homepage.setSex(MapUtils.getString(basicInfoMap, "sexName"));
		 String birthday = MapUtils.getString(basicInfoMap, "birthday");
		 if(StringUtils.isNotBlank(birthday)) {
			 try {
				homepage.setBirthday(DateUtil.dateParse("yyyy-MM-dd", birthday));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 }
		 //年龄
		 homepage.setAge(MapUtils.getInteger(basicInfoMap,"yearOfAge"));
		 //现住址permanentAddressRegionalism
		 Map<String,Object> permanentAddressRegionalism = MapUtils.getMap(basicInfoMap, "permanentAddressRegionalism");
		 if(MapUtils.isNotEmpty(permanentAddressRegionalism)) {
			 homepage.setPresentProvince(MapUtils.getString(permanentAddressRegionalism, "provinceName")); 
			 homepage.setPresentCity(MapUtils.getString(permanentAddressRegionalism, "cityName")); 
			 homepage.setPresentCounty(MapUtils.getString(permanentAddressRegionalism, "countyName")); 
			 homepage.setPresentOther(MapUtils.getString(permanentAddressRegionalism, "extraAddressDesc")); 
		 }
		 //身份证号
		 homepage.setIdCard(MapUtils.getString(basicInfoMap, "idNumber"));
		 homepage.setPactCode(MapUtils.getString(basicInfoMap, "medicalInsuranceNumber"));
		 
		 
		 //入院时间
		 String inTime = MapUtils.getString(basicInfoMap, "inHospitalDateTime");
		 if(StringUtils.isNotBlank(inTime)) {
			 try {
				homepage.setInTime(DateUtil.dateParse("yyyy-MM-dd HH:mm:ss", inTime));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 }
		 //入院科室
		 homepage.setInDeptCode(MapUtils.getString(basicInfoMap, "inDeptCode"));
		 homepage.setInDept(MapUtils.getString(basicInfoMap, "inDeptName"));
		 //出院时间
		 String outTime = MapUtils.getString(basicInfoMap, "outHospitalDateTime");
		 if(StringUtils.isNotBlank(outTime)) {
			 try {
				homepage.setOutDate(DateUtil.dateParse("yyyy-MM-dd HH:mm:ss", outTime));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 }
		 //入院科室
		 homepage.setOutDeptCode(MapUtils.getString(basicInfoMap, "outDeptCode"));
		 homepage.setOutDept(MapUtils.getString(basicInfoMap, "outDeptName"));
		 
		 //入院方式
		 homepage.setInType(MapUtils.getString(basicInfoMap, "inHospitalTypeName"));
		 //离院方式
		 homepage.setOutTypeCode(MapUtils.getString(basicInfoMap, "outHospitalTypeCode"));
		 homepage.setOutType(MapUtils.getString(basicInfoMap, "outHospitalTypeName"));
		 
		 homepage.setInhosDays(MapUtils.getInteger(basicInfoMap, "inHospitalDayNumber"));
		 
		 //诊断
		 Map<String,Object> diseaseDiagInfo = MapUtils.getMap(jsonMap, "diseaseDiagInfo");
		 if(MapUtils.isNotEmpty(diseaseDiagInfo)) {
			 //门诊诊断
			 Map<String,Object> outpatientDiagRecord = MapUtils.getMap(diseaseDiagInfo, "outpatientDiagRecord");
			 if(MapUtils.isNotEmpty(outpatientDiagRecord)) {
				 homepage.setClinicDiagCode(MapUtils.getString(outpatientDiagRecord, "diseaseDiagCode"));
				 homepage.setClinicDiag(MapUtils.getString(outpatientDiagRecord, "diseaseDiagName"));
			 }
			//主要i诊断
			 Map<String,Object> mainDiagRecord = MapUtils.getMap(diseaseDiagInfo, "mainDiagRecord");
			 if(MapUtils.isNotEmpty(mainDiagRecord)) {
				 homepage.setMainDiagCode(MapUtils.getString(mainDiagRecord, "diseaseDiagCode"));
				 homepage.setMainDiag(MapUtils.getString(mainDiagRecord, "diseaseDiagName"));
			 }
			 //其他诊断 diseaseDiagRecords
			 List<Map<String,Object>> diseaseDiagRecords = (List<Map<String, Object>>) MapUtils.getObject(diseaseDiagInfo, "diseaseDiagRecords");
			 int diseaseDiagRecordCount = 1;
			 for (Map<String, Object> diseaseDiagRecord : diseaseDiagRecords) {
				 if(diseaseDiagRecordCount==1) {
					 homepage.setOtherDiag1(MapUtils.getString(diseaseDiagRecord, "diseaseDiagName"));
					 homepage.setOtherDiagCode1(MapUtils.getString(diseaseDiagRecord, "diseaseDiagCode"));
				 }else if(diseaseDiagRecordCount==2) {
					 homepage.setOtherDiag2(MapUtils.getString(diseaseDiagRecord, "diseaseDiagName"));
					 homepage.setOtherDiagCode2(MapUtils.getString(diseaseDiagRecord, "diseaseDiagCode"));
				 }else if(diseaseDiagRecordCount==3) {
					 homepage.setOtherDiag3(MapUtils.getString(diseaseDiagRecord, "diseaseDiagName"));
					 homepage.setOtherDiagCode3(MapUtils.getString(diseaseDiagRecord, "diseaseDiagCode"));
				 }else if(diseaseDiagRecordCount==4) {
					 homepage.setOtherDiag4(MapUtils.getString(diseaseDiagRecord, "diseaseDiagName"));
					 homepage.setOtherDiagCode4(MapUtils.getString(diseaseDiagRecord, "diseaseDiagCode"));
				 }else if(diseaseDiagRecordCount==5) {
					 homepage.setOtherDiag5(MapUtils.getString(diseaseDiagRecord, "diseaseDiagName"));
					 homepage.setOtherDiagCode5(MapUtils.getString(diseaseDiagRecord, "diseaseDiagCode"));
				 }else if(diseaseDiagRecordCount==6) {
					 homepage.setOtherDiag6(MapUtils.getString(diseaseDiagRecord, "diseaseDiagName"));
					 homepage.setOtherDiagCode6(MapUtils.getString(diseaseDiagRecord, "diseaseDiagCode"));
				 }else if(diseaseDiagRecordCount==7) {
					 homepage.setOtherDiag7(MapUtils.getString(diseaseDiagRecord, "diseaseDiagName"));
					 homepage.setOtherDiagCode7(MapUtils.getString(diseaseDiagRecord, "diseaseDiagCode"));
				 }else if(diseaseDiagRecordCount==8) {
					 homepage.setOtherDiag8(MapUtils.getString(diseaseDiagRecord, "diseaseDiagName"));
					 homepage.setOtherDiagCode8(MapUtils.getString(diseaseDiagRecord, "diseaseDiagCode"));
				 }else if(diseaseDiagRecordCount==9) {
					 homepage.setOtherDiag9(MapUtils.getString(diseaseDiagRecord, "diseaseDiagName"));
					 homepage.setOtherDiagCode9(MapUtils.getString(diseaseDiagRecord, "diseaseDiagCode"));
				 }else if(diseaseDiagRecordCount==10) {
					 homepage.setOtherDiag10(MapUtils.getString(diseaseDiagRecord, "diseaseDiagName"));
					 homepage.setOtherDiagCode10(MapUtils.getString(diseaseDiagRecord, "diseaseDiagCode"));
				 }
				 diseaseDiagRecordCount++;
			}
			 
		 }
		 //治疗方案
		 Map<String,Object> cureInfo = MapUtils.getMap(jsonMap, "cureInfo");
		 if(MapUtils.isNotEmpty(cureInfo)) {
			 List<Map<String,Object>> cureWorks = (List<Map<String, Object>>) MapUtils.getObject(cureInfo, "cureWorkers");
			 if(CollectionUtils.isNotEmpty(cureWorks)) {
				 for (Map<String, Object> cureWord : cureWorks) {
					String medicalWorkerCode = MapUtils.getString(cureWord, "medicalWorkerCode");
					String professionTitleCode = MapUtils.getString(cureWord, "professionTitleCode");
					String medicalWorkerName = MapUtils.getString(cureWord, "medicalWorkerName");
					//主治医师
					if(professionTitleCode.equals("A2003")) {
						homepage.setChargeDoc(medicalWorkerName);
						homepage.setChargeDocCode(medicalWorkerCode);
					//住院医师
					}else if(professionTitleCode.equals("A2005")) {
						homepage.setInhosDoc(medicalWorkerName);
						homepage.setInhosDocCode(medicalWorkerCode);
					}
				}
			 }
			 
			 List<Map<String,Object>> diagAccordRecords = (List<Map<String, Object>>) MapUtils.getObject(cureInfo, "diagAccordRecords");
			 if(CollectionUtils.isNotEmpty(diagAccordRecords)) {
				 for (Map<String, Object> diagAccordRecord : diagAccordRecords) {
					 String diagCompareCode = MapUtils.getString(diagAccordRecord, "diagCompareCode");
					 String diagAccordName = MapUtils.getString(diagAccordRecord, "diagAccordName");
					 //门诊诊断与出院诊断
					 if("A01".equals(diagCompareCode)) {
						 homepage.setCliOutAccord(diagAccordName);
					 //入院诊断与出院诊断
					 }else if("A02".equals(diagCompareCode)) {
						 homepage.setInOutAccord(diagAccordName);
					 //术前诊断与术后诊断
					 }else if("A03".equals(diagCompareCode)) {
						 homepage.setOpsAccord(diagAccordName);
					 //临床诊断与病理诊断
					 }else if("A04".equals(diagCompareCode)) {
						 homepage.setCliPatAccord(diagAccordName);
					 //放射诊断与病理诊断
					 }else if("A05".equals(diagCompareCode)) {
						 homepage.setRadiAccord(diagAccordName);
				
					 }
				}
			 }
			 
			 //是否再住院计划
			 homepage.setIsReturn(MapUtils.getString(cureInfo, "rehospitalAimOf31Name"));
			 //抢救次数rescueNumber
			 homepage.setRescueNum(MapUtils.getInteger(cureInfo, "rescueNumber"));
			//抢救成功次数rescueNumber
			 homepage.setSuccessNum(MapUtils.getInteger(cureInfo, "rescueSucceedNumber"));
			 
			 
		 }
		 
		 Map<String,Object> operateInfoMap = MapUtils.getMap(jsonMap, "operateInfo");
		 if(MapUtils.isNotEmpty(operateInfoMap)) {
			 
			List<Map<String,Object>> operateRecords =  (List<Map<String, Object>>) MapUtils.getObject(operateInfoMap, "operateRecords"); 
			
			int operateRecordCount = 1;
			for (Map<String, Object> operateRecord : operateRecords) {
				if(operateRecordCount==1) {
					//手术编码
					homepage.setOperationCode(MapUtils.getString(operateRecord, "operateCode"));
					//手术名称
					homepage.setOperationName(MapUtils.getString(operateRecord, "operateName"));
					String isUnPlanReOperateName = MapUtils.getString(operateRecord, "isUnPlanReOperateName");
					//
					homepage.setIsUnscheduledOps(isUnPlanReOperateName);
					homepage.setOpsType(MapUtils.getString(operateRecord, "patientSourceName"));
					//手术时间
					String opsTime = MapUtils.getString(operateRecord, "operateStartDate");
					if(StringUtils.isNotBlank(opsTime)) {
						try {
							homepage.setOpsDate(DateUtils.parseDate(opsTime, "yyyy-MM-dd HH:mm:ss"));
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					//手术级别
					homepage.setOpsLevel(MapUtils.getString(operateRecord, "opsLevelName"));
					homepage.setOpsLevelCode(MapUtils.getString(operateRecord, "opsLevelCode"));
					//切口登记
					homepage.setOpsCut(MapUtils.getString(operateRecord, "incisionLevelName"));
					homepage.setOpsCutCode(MapUtils.getString(operateRecord, "incisionLevelCode"));
					//愈合等级
					homepage.setOpsHeal(MapUtils.getString(operateRecord, "cicatrizeTypeName"));
					homepage.setOpsHealCode(MapUtils.getString(operateRecord, "cicatrizeTypeCode"));
					//手术医师
					List<Map<String,Object>> operateWorkers = (List<Map<String, Object>>) MapUtils.getObject(operateRecord, "operateWorkers");
					if(CollectionUtils.isNotEmpty(operateWorkers)) {
						//{idNumber=null, medicalWorkerCode=000633, medicalWorkerName=刘胜利, professionTitleCode=A5001, professionTitleName=手术医师}
						for (Map<String, Object> operateWorker : operateWorkers) {
							String professionTitleCode = MapUtils.getString(operateWorker, "professionTitleCode");
							if("A5001".equals(professionTitleCode)) {
								homepage.setOpsDoc(MapUtils.getString(operateWorker, "medicalWorkerName"));
								homepage.setOpsDocCode(MapUtils.getString(operateWorker, "medicalWorkerCode"));
							}
						}
					}
					
				}
				operateRecordCount++;
			}
		 }
		 
		 Map<String,Object> costInfo =MapUtils.getMap(jsonMap, "costInfo");
		 if(MapUtils.isNotEmpty(costInfo)) {
			 String totalCost = MapUtils.getString(costInfo, "totalCost");
			 if(StringUtils.isNotBlank(totalCost)) {
				 homepage.setTotCost(new BigDecimal(totalCost));
			 }
			
		 }
		 
		return homepage;
	}
	
}
